#!/bin/bash

# Script to evaluate effect of background subtraction from TDCs.

for i in $( ls *.bgr );
do
  bgrfile=$i
  echo processing $i
  txtfile=$( echo $i | sed -e "s/bgr/txt/" )
  tx2file=$( echo $i | sed -e "s/bgr/tx2/" )
  dsafile=$( echo $i | sed -e "s/bgr/dsa/" )
  roifile=$( echo $i | sed -e "s/bgr/roi/" )
  pngfile=$( echo $i | sed -e "s/bgr/png/" )
  vmfile=$( echo $i | sed -e "s/bgr/vm/" )
  gt1file=$( echo $i | sed -e "s/bgr/gt1/" )
  gt2file=$( echo $i | sed -e "s/bgr/gt2/" )
  
  # generate TDC without background subtraction
  echo "  "genereate TDC without bgsub
  rm ./$txtfile
  # echo ./dmetry ./$dsafile ./$roifile ./$txtfile ./$vmfile
  ./dmetry ./$dsafile ./$roifile ./$txtfile ./$vmfile
  ./measure ./$txtfile ./$gt1file
  
  # generate TDC with background subtraction
  echo "  "generate TDC with bgsub
  cp ./$txtfile ./$tx2file
  ./bgsub ./$tx2file ./$bgrfile ./$dsafile
  ./measure ./$tx2file ./$gt2file
  
  # construct diagram
  echo "  "generate diagram
  echo "set terminal png" > a.pl
  echo "set output '"$pngfile"'" >> a.pl
  echo "plot '"$txtfile"' with lines, '"$gt1file"' with lines, '"$tx2file"' with lines, '"$gt2file"' with lines" >> a.pl
  gnuplot ./a.pl
done
